package com.lzy.tricbuild2_hash;

import java.util.HashSet;
import java.util.Objects;

public class ValidHashSet {

	public static void main(String[] args) {
		HashSet<Node1> set1 = new HashSet<>();
		set1.add(new Node1(1));
		set1.add(new Node1(1));
		set1.add(new Node1(1));

		System.out.println(set1.size());  // 3

		HashSet<Node2> set2 = new HashSet<>();
		set2.add(new Node2(1));
		set2.add(new Node2(1));
		set2.add(new Node2(1));

		System.out.println(set2.size());  // 重写hashcode,不重写equels时：3;; 重写hashcode,重写equels时：1


	}


	public static class Node1 {
		int a;

		Node1(int a) {
			this.a = a;
		}

		public int getA() {
			return a;
		}

		public void setA(int a) {
			this.a = a;
		}
	}

	public static class Node2 {
		int a;

		Node2(int a) {
			this.a = a;
		}

		public int getA() {
			return a;
		}

		public void setA(int a) {
			this.a = a;
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;
			Node2 node2 = (Node2) o;
			return a == node2.a;
		}

		@Override
		public int hashCode() {
			System.out.println(Objects.hash(a));
			return Objects.hash(a);
		}
	}
}
